package vn.edu.saigontech.ncms.web.teacher;

import java.util.Collection;

import vn.edu.saigontech.ncms.model.enumeration.TeacherType;
import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.Teacher;
import vn.edu.saigontech.ncms.model.teaching.OfferingChecking;
import vn.edu.saigontech.ncms.service.OfferingCheckingService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.TeacherService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class CheckingAttendanceAction extends BaseAction {

	private Long id;
	private Long offeringId;
	private String date;
	private String startTime;
	private String endTime;
	private Long teacherId;
	private String supervisor;
	private String comment;
	
	public String list() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}
	
	public String showAdd() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		REQUEST().setAttribute("offering", offering);
		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		teachers.removeAll(offering.getInstructors());
		REQUEST().setAttribute("teachers", teachers);
		return SUCCESS;
	}
	
	public String add() {		
		OfferingCheckingService.INSTANCE.add(
				this.offeringId, 
				DateUtil.fromString(this.date, this.i18n("format.date")), 
				DateUtil.timeFromString(this.startTime, this.i18n("format.time")),
				DateUtil.timeFromString(this.endTime, this.i18n("format.time")),
				this.teacherId, 
				this.supervisor, 
				this.comment);
		return SUCCESS;
	}
	
	public String showEdit() {
		OfferingChecking offeringChecking = OfferingCheckingService.INSTANCE.findById(id);
		REQUEST().setAttribute("offeringChecking", offeringChecking);
		Collection<Teacher> teachers = TeacherService.INSTANCE.findActiveByTeacherType(TeacherType.INSTRUCTOR);
		teachers.removeAll(offeringChecking.getOffering().getInstructors());
		REQUEST().setAttribute("teachers", teachers);
		return SUCCESS;
	}
	
	public String edit() {
		OfferingCheckingService.INSTANCE.edit(
				this.id, 
				DateUtil.fromString(this.date, this.i18n("format.date")), 
				DateUtil.timeFromString(this.startTime, this.i18n("format.time")),
				DateUtil.timeFromString(this.endTime, this.i18n("format.time")),
				this.teacherId, 
				this.supervisor, 
				this.comment);
		return SUCCESS;
	}
	
	public String delete() {
		OfferingCheckingService.INSTANCE.deleteById(id);
		return SUCCESS;
	}
	
	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public void setTeacherId(Long teacherId) {
		this.teacherId = teacherId;
	}

	public void setSupervisor(String supervisor) {
		this.supervisor = supervisor;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
